const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');

// 获取本地ip，自动打开项目
const OS = require("os")
const getLocalhost = () => {
  try {
    let network = OS.networkInterfaces();
    return network[Object.keys(network)[0]][1].address;
  } catch (error) {
    return "localhost";
  }
}

module.exports = {
  entry: './test/index.js',
  mode: 'development',
  devtool: 'inline-source-map',
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'public/index.html'), // 使用的HTML模板
      filename: 'index.html', // 打包生成到的HTML文件名
    }),
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        { from: path.resolve(__dirname, 'public/Cesium'), to: "Cesium" },
        { from: path.resolve(__dirname, 'public/model'), to: "model" },
        { from: path.resolve(__dirname, 'public/data'), to: "data" },
      ],
    }),
    new VueLoaderPlugin()
  ],
  resolve: {
    alias: {
      "elasticsearch": false
    }
  },
  performance: {
    hints: false
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['vue-style-loader', 'css-loader']
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/,
        use: [
          {
            loader:'url-loader',
            options:{
              limit: 10 * 1024,
              outputPath: 'assets/img',
              esModule: false
            }
          },
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          {
            loader: 'file-loader',
            options:{
              outputPath: 'assets/fonts'
            }
          },
        ]
      },
      {
        test:/\.vue$/,
        loader: 'vue-loader' // ['vue-loader'] => 'vue-loader'
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    port: 8089,
    host: getLocalhost(),
    compress: true,
    open: true,
    // hot: true
  }
};